package com.traffic.between.service;

import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import com.traffic.between.config.BetweenProperties;


public class LuojwyServiceImpl {
	private static Logger logger = Logger.getLogger(LuojwyServiceImpl.class);
	public static LuojwyServiceImpl service = new LuojwyServiceImpl();
	
	private static String HOST_ORDER_PATH = BetweenProperties.LUOJWY_ORDER_URL;
	private static String LUOJWY_APPKEY = BetweenProperties.LUOJWY_APPKEY;
	private static String LUOJWY_ACCOUNT = BetweenProperties.LUOJWY_ACCOUNT;
	
	
	
	public String order(String size, String mobile, String tradeNo, String notifyUrl,String range) {
		
        String account = LUOJWY_ACCOUNT;
        String key = LUOJWY_APPKEY;
        
        //range 0-国内 1-省内 2-统付  对应我们的 省漫 省内 全国
        
        String dateformat = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
       
        String waitSig ="account="+account+"&mobile="+mobile+"&notifyUrl="+notifyUrl+"&orderNo="+tradeNo+"&range="+range+"&size="+size+"&timeStamp="+dateformat+key;
        
        String sign = getMD5(waitSig);
        
        String result = "";
        
      
        List<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();
        pairList.add(new BasicNameValuePair("account", account));
        pairList.add(new BasicNameValuePair("mobile", mobile));
        pairList.add(new BasicNameValuePair("size", size));
        pairList.add(new BasicNameValuePair("range", range));
        pairList.add(new BasicNameValuePair("orderNo", tradeNo));
        pairList.add(new BasicNameValuePair("timeStamp", dateformat));
        pairList.add(new BasicNameValuePair("notifyUrl", notifyUrl));
        pairList.add(new BasicNameValuePair("sign", sign));
     
		HttpPost httpPost = new HttpPost(HOST_ORDER_PATH);
		CloseableHttpClient client = HttpClients.createDefault();
		logger.info("between- 落基伟业订购请求时的参数: "+pairList);
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(pairList, "utf-8"));
			HttpResponse response = client.execute(httpPost);
			int statusCode = response.getStatusLine().getStatusCode();
			logger.info("between- 落基伟业流量订购请求时的响应statusCode: " + statusCode);
			
			if (200 == statusCode) {
				result = EntityUtils.toString(response.getEntity(), "utf-8");
				logger.info("between- 落基伟业流量订购请求时的响应数据是: " + result);
			}
		} catch (Exception e) {
			logger.info("between- 落基伟业流量订购请求时异常: " + e);
		}

	
		return result;
	}

	
	
	
	
	
	
	

	/**
	 * md5加密 ，用于生成sign
	 * 
	 * @param message
	 * @return
	 */
	public static String getMD5(String message) {
		System.out.println("getMD5STR " + message);
		MessageDigest messageDigest = null;
		StringBuffer md5StrBuff = new StringBuffer();
		try {
			messageDigest = MessageDigest.getInstance("MD5");
			messageDigest.reset();
			messageDigest.update(message.getBytes("UTF-8"));

			byte[] byteArray = messageDigest.digest();
			for (int i = 0; i < byteArray.length; i++) {
				if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
					md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
				else
					md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
			}
		} catch (Exception e) {
			throw new RuntimeException();
		}
		System.out.println("getMD5====>" + md5StrBuff.toString().toLowerCase());
		return md5StrBuff.toString().toLowerCase();// 字母小写
	}

	public static void main(String[] args) {
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("account", "");//seqId, productCode, pkey, phoneNo , loginName
		params.put("mobile", "");//seqId, productCode, pkey, phoneNo , loginName
		params.put("size", "");//seqId, productCode, pkey, phoneNo , loginName
		params.put("range", "");//seqId, productCode, pkey, phoneNo , phoneNo
		params.put("orderNo", "");//seqId, productCode, pkey, phoneNo , loginName
		params.put("timeStamp", "");//seqId, productCode, pkey, phoneNo , loginName
		params.put("notifyUrl", "");//seqId, productCode, pkey, phoneNo , loginName
		Object[] keys = params.keySet().toArray();
		Arrays.sort(keys);//升序
		for (Object key : keys) {
			System.out.println(key);
		}
 
		String waitSign = "account=allotment&mobile=13000100001&notifyUrl=http://baidu.com&orderNo=1480583219&range=1&size=100&timeStamp=201612011706591qaz2wsx3edc4rfv5tgb6yhn7ujm8ik9ol0p";
		System.out.println(getMD5(waitSign));
	}

}
